<!--
 * @Author: hucc
 * @Date: 2021-10-14 19:59:53
 * @LastEditors: hucc
 * @LastEditTime: 2021-10-17 20:54:29
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <script src="../js/vuex.js"></script>
    <script src="../js/vue-router.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <script src="../js/axios.js"></script>
    <script src="../js/qs.js"></script>
    <style>
        .head {
            height: 50px;
            font-size: 32px;
            text-align: center;
            background-color: darkgreen;
        }
        
        .main {
            height: 800px;
            display: flex;
            margin-top: 20px;
        }
        
        .left {
            width: 200px;
            border: 3px solid olive;
            margin-right: 30px;
        }
        
        .left:hover {
            cursor: pointer;
        }
        
        .right {
            flex-direction: row;
            flex: 1;
            border: 3px solid blueviolet;
        }
        
        .left1 {
            background-color: aqua;
            text-align: center;
            font-size: 24px;
            height: 50px;
            line-height: 50px;
            border-bottom: 3px solid olive;
        }
        
        .left2 {
            background-color: aqua;
            text-align: center;
            font-size: 24px;
            height: 50px;
            line-height: 50px;
        }
    </style>
</head>

<body>
    <div id="app">
        <router-view></router-view>
        <!-- {{msg1}} -->
    </div>

    <script>
        let login = {
            data() {
                return {
                    loginData: {}
                }
            },
            template: `
        <div>
          <h1>欢迎登陆</h1>  
          用户名：<input type="text" v-model="loginData.username">
          密码：<input type="text" v-model="loginData.password">
          <button @click="loginHandler">登录</button>
        </div>
      `,
            methods: {
                loginHandler() {
                    axios({
                        url: 'http://47.94.46.113:8888/user/login',
                        method: 'post',
                        data: JSON.stringify(this.loginData)
                    }).then(res => {
                        sessionStorage.setItem('token', res.data.token)
                            // 登录成功，跳转至首页
                        this.$router.push({
                            path: '/index'
                        })
                    })
                }
            }
        }

        let index = {
            data() {
                return {}
            },
            template: `
        <div>
          <div class='head'>欢迎使用</div>  
          <div class="main">
              <div class='left'>
                
                  <div class='left1'><router-link to='/category'>{{$store.state.categories.msg1}}</router-link></div>
                <div class='left2'><router-link to='/article'>{{$store.state.articles.msg2}}</router-link></div>
                </div>
              <div class='right'>
                 <router-view></router-view>
                </div>
            </div>
        </div>
      `,

        }
        let category = {
            data() {
                return {
                    categories: [],
                }
            },
            template: `
      <div>
        <el-table :data="categories" style="width:100%">
            <el-table-column type='index' label='序号'></el-table-column>
            <el-table-column prop="id" label='编号'></el-table-column>
            <el-table-column prop='name' label='栏目名称'></el-table-column>
            <el-table-column prop='description' label='栏目描述'></el-table-column>
            <!-- <el-table-column prop='no' label='序号'></el-table-column> -->
            <el-table-column prop='parentId' label='所属栏目'></el-table-column>
        </div>
    `
        }
        let article = {
                data() {
                    return {}
                },
                template: `
      <div>文章管理</div>
    `
            }
            // 定义路由
        let router = new VueRouter({
                routes: [{
                    path: '/',
                    redirect: '/login'
                }, {
                    path: '/login',
                    component: login
                }, {
                    path: '/index',
                    component: index,
                    children: [{
                        path: '/category',
                        component: category
                    }, {
                        path: '/article',
                        component: article
                    }]
                }, ]
            })
            // 设置路由全局前置守卫
        router.beforeEach((to, from, next) => {
                let token = sessionStorage.getItem('token');
                if (to.path == '/index' && !token) {
                    alert('请先登录！');
                    next('/login')
                }
                next();
            })
            //定义模块化对象 栏目管理
        let categories = {
                namespaced: true,
                state() {
                    return {
                        msg1: '栏目管理'
                    }
                },
                getters: {},
                mutations: {

                },
            }
            //定义模块化对象 文章管理
        let articles = {
                namespaced: true,
                state() {
                    return {
                        msg2: '文章管理'
                    }
                },
                getters: {},
                mutations: {

                }
            }
            //定义状态机
        let store = new Vuex.Store({
                modules: {
                    categories,
                    articles
                }
            })
            //全局解构 Vuex
        let {
            mapState,
            mapGetters,
            mapMutations,
            mapActions
        } = Vuex;
        //根实例
        let vm = new Vue({
            el: '#app',
            data: {

            },
            methods: {

            },
            created() {
                console.log(this.$store)
            },
            computed: {
                // ...mapState('categories', ['msg1']),
                // ...mapState('articles', ['msg2']),
            },
            // 注册路由
            router,
            store,
            components: {
                login,
                index
            }
        })
    </script>
</body>

</html>